import axios from 'axios'
import weatherList from '@/assets/weather/weather.json'

export function getWeather () {
    let key = 'ef71319d6ab85c8b4eae4ca73b506e7e'
    return axios
        .get('https://restapi.amap.com/v3/ip', { params: { key }, withCredentials: false })
        .then(({ data }) => {
            const { status, adcode } = data
            if (status != 0 && adcode) {
                return axios.get('https://restapi.amap.com/v3/weather/weatherInfo', {
                    params: { key, city: adcode },
                    withCredentials: false
                }).then(({ data }) => {
                    const { lives } = data
                    const { weather, winddirection, windpower, temperature } = lives[0]
                    let weatherIcon = weatherList.find(item => item.name == weather)
                    let weatherWeather = weatherIcon ? `#${weatherIcon.iconName}` : '#icon-weizhi'
                    return {
                        weatherIcon: weatherWeather,
                        windDirection: winddirection || '-',
                        windPower: windpower || '-',
                        temperature: temperature || '-'
                    };
                })
            }
        })
}
